﻿using System;
using Windows.Devices.Bluetooth;
using Windows.Devices.Bluetooth.GenericAttributeProfile;
using Windows.Storage.Streams;


//https://docs.microsoft.com/en-us/windows/uwp/devices-sensors/gatt-server

namespace App1
{ 

public class BLEAdv
{
    public GattServiceProvider serviceProvider;
    public GattLocalCharacteristic _readCharacteristic;
    public GattLocalCharacteristic _notifyCharacteristic;
    public GattLocalCharacteristic _writeCharacteristic;
    public GattLocalCharacteristicParameters WriteParameters;
    public GattLocalCharacteristicParameters NotifyParameters;
    public GattLocalCharacteristicParameters ReadParameters;

    public BLEAdv()
	{
	}

    public void publish()
    {
        GattServiceProviderAdvertisingParameters advParameters = new GattServiceProviderAdvertisingParameters
        {
            IsDiscoverable = true,
            IsConnectable = true
        };
        serviceProvider.StartAdvertising(advParameters);
    }

    public async void create_serviceAsync()
    {
        //"\x1b\xc5\xd5\xa5\x02\x00\xa6\x85\xe5\x11\x35\x39\xa0\xbb\x5a\xfd"
        //"1bc5d5a50200a685e5113539a0bb5afd"

        Guid uuid = new Guid("1bc5d5a50200a685e5113539a0bb5afd");
        GattServiceProviderResult result = await GattServiceProvider.CreateAsync(uuid);

        if (result.Error == BluetoothError.Success)
        {
            serviceProvider = result.ServiceProvider;
            // 
        }
    }

        public async void create_notify_service()
    {
        Guid uuid3 = new Guid("1bc5d5a50200a685e5113539a0bb5afd");
            GattLocalCharacteristicParameters NotifyParameters = new GattLocalCharacteristicParameters();
            NotifyParameters.CharacteristicProperties = GattCharacteristicProperties.Notify;
            //GattCharacteristicProperties.
            //NotifyParameters.
            GattLocalCharacteristicResult characteristicResult = await serviceProvider.Service.CreateCharacteristicAsync(uuid3, NotifyParameters);
        if (characteristicResult.Error != BluetoothError.Success)
        {
            // An error occurred.
            return;
        }
        _notifyCharacteristic = characteristicResult.Characteristic;
        _notifyCharacteristic.SubscribedClientsChanged += SubscribedClientsChanged;
    }
        public async void testAsync()
    {
        //GattLocalCharacteristicResult characteristicResult = await serviceProvider.Service.CreateCharacteristicAsync(uuid1, ReadParameters);
        //if (characteristicResult.Error != BluetoothError.Success)
        //{
        //    // An error occurred.
        //    return;
        //}
        //_readCharacteristic = characteristicResult.Characteristic;
        //_readCharacteristic.ReadRequested += ReadCharacteristic_ReadRequested;

        //characteristicResult = await serviceProvider.Service.CreateCharacteristicAsync(uuid2, WriteParameters);
        //if (characteristicResult.Error != BluetoothError.Success)
        //{
        //    // An error occurred.
        //    return;
        //}
        //_writeCharacteristic = characteristicResult.Characteristic;
        //_writeCharacteristic.WriteRequested += WriteCharacteristic_WriteRequested;

        //characteristicResult = await serviceProvider.Service.CreateCharacteristicAsync(uuid3, NotifyParameters);
        //if (characteristicResult.Error != BluetoothError.Success)
        //{
        //    // An error occurred.
        //    return;
        //}
        //_notifyCharacteristic = characteristicResult.Characteristic;
        //_notifyCharacteristic.SubscribedClientsChanged += SubscribedClientsChanged;
    }

    public void SubscribedClientsChanged(GattLocalCharacteristic sender, object args)
    {
        throw new NotImplementedException();
    }

    public void WriteCharacteristic_WriteRequested(GattLocalCharacteristic sender, GattWriteRequestedEventArgs args)
    {
        throw new NotImplementedException();
    }

    public void ReadCharacteristic_ReadRequested(GattLocalCharacteristic sender, GattReadRequestedEventArgs args)
    {
        throw new NotImplementedException();
    }

    async void NotifyValue()
    {
        var writer = new DataWriter();
        // Populate writer with data
        // ...
        writer.WriteString("hello world\n");

        await _notifyCharacteristic.NotifyValueAsync(writer.DetachBuffer());
    }
}
}